<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/meow.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/meow.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/blue/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"meowxue.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":true,"version":"8.18.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="概述大多数的linux发行版都默认安装了corn服务和crontab工具。cron服务是一个用于执行定时任务的后台服务，而crontab是用于编辑和管理用户的定时任务的命令行工具。通过crontab工具，用户可以添加、编辑、删除定时任务的执行时间和要运行的命令或者脚本。 利用定时执行备份的shell脚本。">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux服务器中Oracle数据库定时备份">
<meta property="og:url" content="https://meowxue.gitee.io/2023/08/30/%E6%8A%80%E8%83%BD/Linux%E4%B8%ADOracle%E6%95%B0%E6%8D%AE%E5%BA%93%E8%87%AA%E5%8A%A8%E5%A4%87%E4%BB%BD/index.html">
<meta property="og:site_name" content="喵小哥儿の博客">
<meta property="og:description" content="概述大多数的linux发行版都默认安装了corn服务和crontab工具。cron服务是一个用于执行定时任务的后台服务，而crontab是用于编辑和管理用户的定时任务的命令行工具。通过crontab工具，用户可以添加、编辑、删除定时任务的执行时间和要运行的命令或者脚本。 利用定时执行备份的shell脚本。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-08-30T04:03:00.000Z">
<meta property="article:modified_time" content="2023-09-25T09:46:17.591Z">
<meta property="article:author" content="喵小哥儿">
<meta property="article:tag" content="linux">
<meta property="article:tag" content="oracle">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://meowxue.gitee.io/2023/08/30/%E6%8A%80%E8%83%BD/Linux%E4%B8%ADOracle%E6%95%B0%E6%8D%AE%E5%BA%93%E8%87%AA%E5%8A%A8%E5%A4%87%E4%BB%BD/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://meowxue.gitee.io/2023/08/30/%E6%8A%80%E8%83%BD/Linux%E4%B8%ADOracle%E6%95%B0%E6%8D%AE%E5%BA%93%E8%87%AA%E5%8A%A8%E5%A4%87%E4%BB%BD/","path":"2023/08/30/技能/Linux中Oracle数据库自动备份/","title":"Linux服务器中Oracle数据库定时备份"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Linux服务器中Oracle数据库定时备份 | 喵小哥儿の博客</title>
  







<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/theme-next/theme-next-needmoreshare2@1/needsharebutton.min.css"><style>
#needsharebutton-postbottom {
  cursor: pointer;
  height: 26px;
  margin-top: 10px;
  position: relative;
}
#needsharebutton-postbottom .btn {
  border: 1px solid $btn-default-border-color;
  border-radius: 3px;
  display: initial;
  padding: 1px 4px;
}
</style>
  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">喵小哥儿の博客</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-shuoshuo"><a href="/shuoshuo/" rel="section"><i class="fa fa-comment fa-fw"></i>碎念</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%A6%82%E8%BF%B0"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#shell%E8%84%9A%E6%9C%AC"><span class="nav-number">2.</span> <span class="nav-text">shell脚本</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B3%A8%E6%84%8F%E7%9A%84%E7%82%B9%E5%92%8C%E7%9B%B8%E5%85%B3%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-number">2.1.</span> <span class="nav-text">注意的点和相关的问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E5%86%8D%E6%AC%A1%E8%AE%BE%E7%BD%AE%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="nav-number">2.1.1.</span> <span class="nav-text">1.为什么要再次设置环境变量?</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%AE%E5%BD%95%E5%AF%B9%E8%B1%A1"><span class="nav-number">2.1.2.</span> <span class="nav-text">2.数据库目录对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E8%84%9A%E6%9C%AC%E5%AE%8C%E6%88%90%E5%90%8E"><span class="nav-number">2.1.3.</span> <span class="nav-text">3.脚本完成后</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AE%9A%E6%97%B6"><span class="nav-number">3.</span> <span class="nav-text">定时</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="喵小哥儿"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">喵小哥儿</p>
  <div class="site-description" itemprop="description">慢热，沉默，抽烟，小酌，喜欢独处，半点斯文，半点猥琐。</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">32</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="mailto:meowxue@163.com" title="E-Mail → mailto:meowxue@163.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://meowxue.gitee.io/2023/08/30/%E6%8A%80%E8%83%BD/Linux%E4%B8%ADOracle%E6%95%B0%E6%8D%AE%E5%BA%93%E8%87%AA%E5%8A%A8%E5%A4%87%E4%BB%BD/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="喵小哥儿">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="喵小哥儿の博客">
      <meta itemprop="description" content="慢热，沉默，抽烟，小酌，喜欢独处，半点斯文，半点猥琐。">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Linux服务器中Oracle数据库定时备份 | 喵小哥儿の博客">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Linux服务器中Oracle数据库定时备份
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-08-30 12:03:00" itemprop="dateCreated datePublished" datetime="2023-08-30T12:03:00+08:00">2023-08-30</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%8A%80%E8%83%BD/" itemprop="url" rel="index"><span itemprop="name">技能</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>1.3k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>5 分钟</span>
    </span>
</div>

          
          
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h1 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h1><p>大多数的linux发行版都默认安装了corn服务和crontab工具。cron服务是一个用于执行定时任务的后台服务，而crontab是用于编辑和管理用户的定时任务的命令行工具。通过crontab工具，用户可以添加、编辑、删除定时任务的执行时间和要运行的命令或者脚本。</p>
<p>利用定时执行备份的shell脚本。</p>
<span id="more"></span>

<h1 id="shell脚本"><a href="#shell脚本" class="headerlink" title="shell脚本"></a>shell脚本</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#配置数据库的环境变量</span></span><br><span class="line"><span class="built_in">export</span> ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/db_1/</span><br><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$ORACLE_HOME</span>/bin:<span class="variable">$PATH</span></span><br><span class="line"><span class="comment">#备份文件存放位置</span></span><br><span class="line">BACKUP=/usr/local/DBback/</span><br><span class="line"><span class="comment">#数据库目录对象</span></span><br><span class="line">DIRECTORY=HRPBACKUP</span><br><span class="line"><span class="comment">#当前时间</span></span><br><span class="line">DATETIME=$(<span class="built_in">date</span> +%Y-%m-%d_%H%M%S)</span><br><span class="line"><span class="built_in">echo</span> <span class="variable">$DATETIME</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#数据库地址</span></span><br><span class="line">HOST=127.0.0.1/orcl</span><br><span class="line">USERNAME=username</span><br><span class="line">PASSWORD=123456</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#创建备份目录，如果不存在就创建</span></span><br><span class="line">[ ! -d <span class="string">&quot;<span class="variable">$&#123;BACKUP&#125;</span>&quot;</span> ] &amp;&amp; <span class="built_in">mkdir</span> -p <span class="string">&quot;<span class="variable">$&#123;BACKUP&#125;</span>&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#打印导出命令</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;expdp <span class="variable">$&#123;USERNAME&#125;</span>/<span class="variable">$&#123;PASSWORD&#125;</span>@<span class="variable">$&#123;HOST&#125;</span> directory=<span class="variable">$&#123;DIRECTORY&#125;</span> dumpfile=<span class="variable">$&#123;DATETIME&#125;</span>_back.dmp logfile=<span class="variable">$&#123;DATETIME&#125;</span>.log schemas=<span class="variable">$&#123;USERNAME&#125;</span> compression=all&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#备份数据库</span></span><br><span class="line">expdp <span class="variable">$&#123;USERNAME&#125;</span>/<span class="variable">$&#123;PASSWORD&#125;</span>@<span class="variable">$&#123;HOST&#125;</span> directory=<span class="variable">$&#123;DIRECTORY&#125;</span> dumpfile=<span class="variable">$&#123;DATETIME&#125;</span>_back.dmp logfile=<span class="variable">$&#123;DATETIME&#125;</span>.<span class="built_in">log</span> schemas=<span class="variable">$&#123;USERNAME&#125;</span> compression=all</span><br><span class="line"></span><br><span class="line"><span class="comment">#删除10天前的数据</span></span><br><span class="line">find <span class="variable">$&#123;BACKUP&#125;</span> -atime +10 -name <span class="string">&quot;*_back.dmp&quot;</span> -<span class="built_in">exec</span> <span class="built_in">rm</span> -ef &#123;&#125; \;</span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;备份完成&quot;</span></span><br></pre></td></tr></table></figure>
<h2 id="注意的点和相关的问题"><a href="#注意的点和相关的问题" class="headerlink" title="注意的点和相关的问题"></a>注意的点和相关的问题</h2><h3 id="1-为什么要再次设置环境变量"><a href="#1-为什么要再次设置环境变量" class="headerlink" title="1.为什么要再次设置环境变量?"></a>1.为什么要再次设置环境变量?</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#配置数据库的环境变量</span></span><br><span class="line"><span class="built_in">export</span> ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/db_1/</span><br><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$ORACLE_HOME</span>/bin:<span class="variable">$PATH</span></span><br></pre></td></tr></table></figure>
<p>cron运行的环境和正常的shell执行环境不同。在正常情况下直接执行备份的shell脚本 expdp 命令可以被正确识别，但放到 crontab 中，expdp 命令将会出错，错误信息大概如下：</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">UDE-00013: Message 13 <span class="keyword">not</span> found; <span class="literal">No</span> message file <span class="keyword">for</span> <span class="attribute">product</span>=RDBMS, <span class="attribute">facility</span>=UDE</span><br><span class="line">UDE-00019: You may need <span class="keyword">to</span> <span class="built_in">set</span> ORACLE_HOME <span class="keyword">to</span> your Oracle software directory</span><br></pre></td></tr></table></figure>

<p>export 将变量设置为环境变量，并且在当前shell会话及子进程中可见和可访问。</p>
<h3 id="2-数据库目录对象"><a href="#2-数据库目录对象" class="headerlink" title="2.数据库目录对象"></a>2.数据库目录对象</h3><p>此处为oracle的文件目录对象,类似于将路径起个变量名，并将这种对应关系保存在数据库中。</p>
<p>查询所有文件目录</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> directory_name, directory_path <span class="keyword">FROM</span> all_directories;</span><br></pre></td></tr></table></figure>

<p>查询结果中的 directory_path 要与 脚本中的 BACKUP 路径一致。</p>
<p>创建文件目录</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> DIRECTORY hrpbackup <span class="keyword">AS</span> <span class="string">&#x27;备份文件要存放的路径&#x27;</span>;</span><br></pre></td></tr></table></figure>

<p>授予公共用户对目录（directory）的读取和写入权限。（非必须）</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">grant</span> read,write <span class="keyword">on</span> directory 目录对象 <span class="keyword">to</span> public;</span><br></pre></td></tr></table></figure>

<h3 id="3-脚本完成后"><a href="#3-脚本完成后" class="headerlink" title="3.脚本完成后"></a>3.脚本完成后</h3><p>给脚本添加可执行权限</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">chmod</span> +x 备份.sh</span><br></pre></td></tr></table></figure>

<p>创建的目录 BACKUP 也需要设置权限</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">chmod</span> 777 /usr/local/DBback</span><br></pre></td></tr></table></figure>

<h1 id="定时"><a href="#定时" class="headerlink" title="定时"></a>定时</h1><p>查询当前所有的定时任务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">crontab -l</span><br></pre></td></tr></table></figure>

<p>编辑定时任务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">crontab -e</span><br></pre></td></tr></table></figure>
<p>crontab 相关命令</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">(<span class="number">1</span>) 语法</span><br><span class="line">crontab [-u &lt;用户名称&gt;][配置文件] 或 crontab &#123; -l | -r | -e &#125;</span><br><span class="line">-u   <span class="comment">#&lt;用户名称&gt; 是指设定指定&lt;用户名称&gt;的定时任务，这个前提是你必须要有其权限(比如说是 root)才能够指定他人的时程表。如果不使用 -u user 的话，就是表示设定自己的定时任务。</span></span><br><span class="line">-l 　<span class="comment">#列出该用户的定时任务设置。</span></span><br><span class="line">-r 　<span class="comment">#删除该用户的定时任务设置。</span></span><br><span class="line">-e 　<span class="comment">#编辑该用户的定时任务设置。</span></span><br><span class="line">(<span class="number">2</span>) 命令时间格式：</span><br><span class="line">*     * 　  *　  *　  *　　command</span><br><span class="line">分　  时　  日　  月　 周　  命令</span><br><span class="line"></span><br><span class="line">第<span class="number">1</span>列表示分钟<span class="number">1</span>～<span class="number">59</span> 每分钟用*或者 */<span class="number">1</span>表示</span><br><span class="line">第<span class="number">2</span>列表示小时<span class="number">1</span>～<span class="number">23</span>（<span class="number">0</span>表示<span class="number">0</span>点）</span><br><span class="line">第<span class="number">3</span>列表示日期<span class="number">1</span>～<span class="number">31</span></span><br><span class="line">第<span class="number">4</span>列表示月份<span class="number">1</span>～<span class="number">12</span></span><br><span class="line">第<span class="number">5</span>列标识号星期<span class="number">0</span>～<span class="number">6</span>（<span class="number">0</span>表示星期天）</span><br><span class="line">第<span class="number">6</span>列要运行的命令</span><br><span class="line">(<span class="number">3</span>)一些Crontab定时任务例子：</span><br><span class="line"><span class="number">30</span> <span class="number">21</span> * * * <span class="regexp">/usr/</span>local<span class="regexp">/etc/</span>rc.d/lighttpd restart  <span class="comment">#每晚的21:30 重启apache</span></span><br><span class="line"><span class="number">45</span> <span class="number">4</span> <span class="number">1</span>,<span class="number">10</span>,<span class="number">22</span> * * <span class="regexp">/usr/</span>local<span class="regexp">/etc/</span>rc.d/lighttpd restart  <span class="comment">#每月1、10、22日的4 : 45重启apache</span></span><br><span class="line"><span class="number">10</span> <span class="number">1</span> * * <span class="number">6</span>,<span class="number">0</span> <span class="regexp">/usr/</span>local<span class="regexp">/etc/</span>rc.d/lighttpd restart  <span class="comment">#每周六、周日的1 : 10重启apache</span></span><br><span class="line"><span class="number">0</span>,<span class="number">30</span> <span class="number">18</span>-<span class="number">23</span> * * * <span class="regexp">/usr/</span>local<span class="regexp">/etc/</span>rc.d/lighttpd restart  <span class="comment">#每天18 : 00至23 : 00之间每隔30分钟重启apache</span></span><br><span class="line"><span class="number">0</span> <span class="number">23</span> * * <span class="number">6</span> <span class="regexp">/usr/</span>local<span class="regexp">/etc/</span>rc.d/lighttpd restart  <span class="comment">#每星期六的11 : 00 pm重启apache</span></span><br><span class="line">* <span class="number">23</span>-<span class="number">7</span><span class="regexp">/1 * * * /u</span>sr<span class="regexp">/local/</span>etc<span class="regexp">/rc.d/</span>lighttpd restart  <span class="comment">#晚上11点到早上7点之间，每隔一小时重启apache</span></span><br><span class="line">* *<span class="regexp">/1 * * * /u</span>sr<span class="regexp">/local/</span>etc<span class="regexp">/rc.d/</span>lighttpd restart  <span class="comment">#每一小时重启apache</span></span><br><span class="line"><span class="number">0</span> <span class="number">11</span> <span class="number">4</span> * mon-wed <span class="regexp">/usr/</span>local<span class="regexp">/etc/</span>rc.d/lighttpd restart  <span class="comment">#每月的4号与每周一到周三的11点重启apache</span></span><br><span class="line"><span class="number">0</span> <span class="number">4</span> <span class="number">1</span> jan * <span class="regexp">/usr/</span>local<span class="regexp">/etc/</span>rc.d/lighttpd restart  <span class="comment">#一月一号的4点重启apache</span></span><br><span class="line"> </span><br><span class="line">*<span class="regexp">/30 * * * * /u</span>sr<span class="regexp">/sbin/</span>ntpdate cn.pool.ntp.org  <span class="comment">#每半小时同步一下时间</span></span><br><span class="line"><span class="number">0</span> *<span class="regexp">/2 * * * /</span>sbin/service httpd restart  <span class="comment">#每两个小时重启一次apache </span></span><br><span class="line"><span class="number">50</span> <span class="number">7</span> * * * <span class="regexp">/sbin/</span>service sshd start  <span class="comment">#每天7：50开启ssh服务 </span></span><br><span class="line"><span class="number">50</span> <span class="number">22</span> * * * <span class="regexp">/sbin/</span>service sshd stop  <span class="comment">#每天22：50关闭ssh服务 </span></span><br><span class="line"><span class="number">0</span> <span class="number">0</span> <span class="number">1</span>,<span class="number">15</span> * * fsck <span class="regexp">/home  #每月1号和15号检查/</span>home 磁盘 </span><br><span class="line"><span class="number">1</span> * * * * <span class="regexp">/home/</span>bruce<span class="regexp">/backup  #每小时的第一分执行 /</span>home<span class="regexp">/bruce/</span>backup这个文件 </span><br><span class="line"><span class="number">00</span> <span class="number">03</span> * * <span class="number">1</span>-<span class="number">5</span> find <span class="regexp">/home &quot;*.xxx&quot; -mtime +4 -exec rm &#123;&#125; \;  #每周一至周五3点钟，在目录/</span>home中，查找文件名为*.xxx的文件，并删除<span class="number">4</span>天前的文件。</span><br><span class="line"><span class="number">30</span> <span class="number">6</span> */<span class="number">10</span> * * ls  <span class="comment">#每月的1、11、21、31日是的6：30执行一次ls命令</span></span><br></pre></td></tr></table></figure>

<p>例如：每天晚上11点30分执行定时备份脚本 &#x2F;usr&#x2F;local&#x2F;backup.sh</p>
<p>路径要写全</p>
<figure class="highlight basic"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="symbol">30 </span><span class="number">11</span> * * * /<span class="keyword">usr</span>/local/backup.sh</span><br></pre></td></tr></table></figure>

<p>crontab在线工具 <a target="_blank" rel="noopener" href="https://tool.lu/crontab">https://tool.lu/crontab</a></p>

    </div>

    
    
    

    <footer class="post-footer"><div class="post-widgets">
      <div id="needsharebutton-postbottom">
        <span class="btn">
          <i class="fa fa-share-alt" aria-hidden="true"></i>
        </span>
      </div>
    </div>
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>喵小哥儿
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://meowxue.gitee.io/2023/08/30/%E6%8A%80%E8%83%BD/Linux%E4%B8%ADOracle%E6%95%B0%E6%8D%AE%E5%BA%93%E8%87%AA%E5%8A%A8%E5%A4%87%E4%BB%BD/" title="Linux服务器中Oracle数据库定时备份">https://meowxue.gitee.io/2023/08/30/技能/Linux中Oracle数据库自动备份/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/linux/" rel="tag"># linux</a>
              <a href="/tags/oracle/" rel="tag"># oracle</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/08/25/%E6%8A%80%E8%83%BD/windows%E6%95%B0%E6%8D%AE%E5%BA%93Oracle%E8%87%AA%E5%8A%A8%E5%A4%87%E4%BB%BD/" rel="prev" title="windows服务器中Oracle数据库定时备份">
                  <i class="fa fa-angle-left"></i> windows服务器中Oracle数据库定时备份
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/09/01/%E6%8A%80%E5%B7%A7/SikuliX%E5%9B%BE%E5%83%8F%E8%AF%86%E5%88%AB/" rel="next" title="SikuliX图像识别">
                  SikuliX图像识别 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">喵小哥儿</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">25k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">1:29</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  <script src="/js/third-party/pace.js"></script>


  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





  <script src="//cdn.jsdelivr.net/gh/theme-next/theme-next-needmoreshare2@1/needsharebutton.min.js"></script>
  <script>
      pbOptions = {};
        pbOptions.iconStyle = "box";
        pbOptions.boxForm = "horizontal";
        pbOptions.position = "bottomCenter";
        pbOptions.networks = "Wechat,QQZone,Weibo,Douban,Twitter,Facebook";
      new needShareButton('#needsharebutton-postbottom', pbOptions);
  </script>
</body>
</html>
